<?php

namespace app\crm\model;

use think\facade\Db;
use app\common\traits\ActionRecordTrait;
use think\Model;
use app\admin\controller\ApiCommon;
use app\mini\model\PersonCase;
use app\crm\model\ReserveandLost;

class StaffModel extends Model
{
	use ActionRecordTrait;

    /**
     * 表名
     * @var string
     */
    protected $name = 'case';
    protected $deleteTime = 'deleted_at';
    protected $defaultSoftDelete = 0;


    /**
     * 客户详情-新增优案
     * @param  array $request 请求参数
     * @return array
     * @author hz
     * @create 2021-10-26
     */
    public function getStaffBaoyou($request)
    {

        

        $today = date('Y-m-d',time());
        
        $staff_ids = $request['staff_ids'] ?? [];

        // echo "<pre>";
        // print_r($staff_ids);
        // echo "</pre>";
        // exit;
        $shop_id = $request['shop_id'] ?? "";

        if(!$staff_ids){
            return resultArray(['error' => "员工列表为空！！！"]);
        }

        if(!$shop_id){
            return resultArray(['error' => "查询门店为空！！！"]);
        }

        $shop_ids = [];
        array_push($shop_ids,$shop_id);

        // echo "<pre>";
        // print_r($request);
        // echo "</pre>";
        // exit;

        

        $model = new ReserveandLost();

        $list = [];
        foreach ($staff_ids as $key => $value) {

            $req = [];

            $req['nurse_id'] = $value['id'];
            $req['cj_type'] = $request['search']['cj_type'] ?? '0';
            
            // 成交客户
            $cj_customer = $model->cj_customer($shop_ids,$req);

            // 保有顾客数
			$baoyou_customer = $model->baoyou_customer($cj_customer,$today);

            // 流失顾客数
			$lost_customer = $model->lost_customer($cj_customer,$today);

            // 预警顾客数
            $warn_customer = $model->warning_customer($cj_customer,$today,'','','');

            // $one = [];

            $staff_ids[$key]['cj_customer'] = count($cj_customer);
            $staff_ids[$key]['baoyou_customer'] = count($baoyou_customer);
            $staff_ids[$key]['lost_customer'] = count($lost_customer);
            $staff_ids[$key]['warn_customer'] = count($warn_customer);

            $staff_ids[$key]['baoyou_rate'] = count($cj_customer) > 0 ? sprintf("%.2f", ((count($baoyou_customer)/count($cj_customer))*100)).'%' : '0.00%';
            // array_push($list,$one);
            // echo "<pre>";
            // print_r($one);
            // echo "</pre>";
            // exit;
        }
        // echo "<pre>";
        // print_r($list);
        // echo "</pre>";
        // exit;

        return $staff_ids;
        
    }

}